package com.example.ec.controller;

import com.example.ec.model.Commodity;
import com.example.ec.service.CommodityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.nio.channels.SeekableByteChannel;

@Controller
@RequestMapping("commodity")
public class CommodityController extends BaseController {

    private final CommodityService commodityService;

    public CommodityController(CommodityService commodityService) {
        this.commodityService = commodityService;
    }

    @PostMapping("create")
    private String create(Commodity commodity) {
        commodityService.create(commodity);
        return "redirect:/commodity/queryAll";
    }

    @GetMapping("queryAll")
    private String queryAll() {
        session.setAttribute("commodities", commodityService.queryAll());
        return "redirect:/admin/list.jsp";
    }


    @GetMapping("queryById/{id}")
    private String queryById(@PathVariable("id") int id) {
        session.setAttribute("commodity", commodityService.queryById(id));
        return "redirect:/admin/edit.jsp";
    }

    @GetMapping("removeById/{id}")
    private String removeById(@PathVariable("id") int id) {
        commodityService.removeById(id);
        return "redirect:/commodity/queryAll";
    }

    @PostMapping("modify")
    private String modify(Commodity commodity){
        commodityService.modify(commodity);
        return "redirect:/commodity/queryAll";
    }
    @PostMapping("query")
    private String query(Commodity commodity){
        session.setAttribute("indexCommodity",commodityService.queryList("query",commodity));
        return "redirect:/user/list.jsp";
    }
}
}
